<!DOCTYPE html>
<html>
<head>
<title>商品管理</title>
#include("../include/common.html")
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>商品管理</h5>
            </div>
            <form id="listForm" action="list" method="get">
            	<input type="hidden" name="type" id="type" value="#(type)">
                <div class="ibox-content">
                    <!-- 搜索 start -->
                    <div class="form-horizontal clear">
                        <div class="form-group">
                            <label class="col-sm-1 control-label">名称：</label>
                            <div class="col-sm-2"><input class="form-control" type="text" name="name" value="#(name)"></div>
                            <label class="col-sm-1 control-label">商品分类：</label>
                            <div class="col-sm-2">
                               <select class="form-control" name="productCategoryId">
                                    <option value="">根目录</option>
			                        #for(productCategoryItem : productCategoryTree)
			                            <option value="#(productCategoryItem.id)" #if(productCategoryItem.id == productCategoryId) selected="selected" #end>
			                                #if(productCategoryItem.grade != 0)
			                                    #for(i = 0; i < productCategoryItem.grade; i++)
			                                        &nbsp;&nbsp;
			                                    #end
			                                #end
			                                #(productCategoryItem.name)
			                            </option>
			                        #end
                                </select>
                            </div>
                            <label class="col-sm-1 control-label">品牌：</label>
                            <div class="col-sm-2">
                               <select class="form-control" name="brandId">
                                    <option value="">--请选择--</option>
			                        #for(brand : brands)
			                            <option value="#(brand.id)" #if(brand.id == brandId) selected="selected" #end>
			                                #(brand.name)
			                            </option>
			                        #end
                                </select>
                            </div>
                        </div>
                        <div class="text-center">
                            <input type="submit" class="btn btn-primary btn-w-m" value="查 询">
                            <input type="reset" class="btn btn-primary btn-w-m" value="重 置">
                        </div>
                    </div>
                    <!-- 搜索 end -->
                    <!-- 列表 start -->
                    <div class="form-horizontal clear">
                        <div class="form-group">
                            <div class="col-sm-8">
                                <a class="btn btn-primary" href="add"><i class="fa fa-plus"></i> 添加</a>
                                <a href="javascript:;" class="btn btn-warning disabled" id="deleteButton"><i class="fa fa-trash"></i> 删除</a>
                                <a href="javascript:;" class="btn btn-primary" id="refreshButton"><i class="fa fa-refresh"></i> 刷新</a>
                                <a href="javascript:;" class="btn btn-primary" id="sortButton"><i class="fa fa-sort"></i> 排序</a>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            	<a href="export" class="btn btn-primary"><i class="fa fa-down"></i> 导出excel</a>
                            		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            		#if(type=='SELL' || type=='SELL_OUT')
                            		<a class="btn btn-primary" href="javascript:;" id="downMarket">下架</a>
                            		#end
                            		#if(type=='OFF_SHELF')
                            		<a class="btn btn-primary" href="javascript:;" id="upMarket">上架</a>
                            		#end
                            </div>
                        </div>
                        <div class="tabs-container">
		                   <ul class="nav nav-tabs">
		                        <li #if(selectType==1)class="active"#end><a data-toggle="tab" href="javascript:void(0)" onclick="typeSearch('SELL')">出售中</a></li>
		                        <li #if(selectType==2)class="active"#end><a data-toggle="tab" href="javascript:void(0)" onclick="typeSearch('SELL_OUT')">已售馨</a></li>
		                        <li #if(selectType==3)class="active"#end><a data-toggle="tab" href="javascript:void(0)" onclick="typeSearch('OFF_SHELF')">下架中</a></li>
		                    </ul>
		                    <div class="tab-content">
		                        <div class="tab-pane active">
		                            <div class="panel-body form-horizontal">
				                        <div class="table-responsive">
				                            <table class="table table-bordered" id="listTable">
				                                <thead>
				                                    <tr>
				                                        <th width="40" class="text-center"><input type="checkbox" id="selectAll" /></th>
				                                        <th>ID</th>
				                                        <th>商品分类</th>
				                                        <th>商品名称</th>
				                                        <th>商品图片</th>
				                                        <th>品牌</th>
				                                        <th>商品价格</th>
				                                        <th>市场价</th>
				                                        <th>成本价</th>
				                                        <th>是否上架</th>
				                                        <th>库存</th>
				                                        <th>标签</th>
				                                        <th width="80">排序</th>
				                                        <th>创建日期</th>
				                                        <th>操作</th>
				                                    </tr>
				                                </thead>
				                                <tbody id="listTr">
				                                    #for(product : page.list)
				                                    <tr>
				                                        <td class="text-center"><input type="checkbox" name="ids" value="#(product.id)" /></td>
				                                        <td>#(product.id)</td>
				                                        <td>#(product.productCategory.name)</td>
				                                        <td>#(product.name)</td>
				                                        <td><img src="#(imageUrl(product.image))" height="50"/></td>
				                                        <td>#(product.brand.name)</td>
				                                        <td>#(product.price)</td>
				                                        <td>#(product.marketPrice)</td>
				                                        <td>#(product.cost)</td>
				                                        <td>#(product.isMarketable?"是":"否")</td>
				                                        <td>#(product.stock)</td>
				                                        <td>#(product.tag)</td>
				                                        <td><input type="text" class="form-control" name="sorts" value="#(product.sort)" data-oldsort="#(product.sort)" data-id="#(product.id)"/></td>
				                                        <td>#date(product.createDate,"yyyy-MM-dd")</td>
				                                        <td>
				                                            <a href="edit?id=#(product.id)">[编辑]</a>
				                                        </td>
				                                    </tr>
				                                    #end
				                                </tbody>
				                            </table>
				                        </div>
			                        </div>
		                        </div>
	                        </div>
                        </div>
                        <div class="text-center">
                            #pagination(pageNumber = page.pageNumber,totalPages = page.totalPage,pattern = "")
                                #include("../include/pagination.html")
                            #end
                        </div>
                    </div>
                    <!-- 列表 end -->
                </div>
            </form>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
$().ready(function() {

    var $sortButton = $("#sortButton");
    
    //排序
    $sortButton.click(function() {
        var $this = $(this);
        var $sortArry = [];
        $.each($("#listTr tr"),function(index,item){
            var $sorts = $(item).find("input[name='sorts']");
            var $sort = $sorts.val();
            var $oldsort = $sorts.data("oldsort");
            var $id = $sorts.data("id");
            if($sort!=$oldsort){
                var $sortReg = /^[1-9]\d*|0$/;
                if (!($sortReg.test($sort))) { 
                   $sort = "0";
                }
                var $json = {"id":$id,"sort":$sort};
                $sortArry.push($json);
            }
        });
        if($sortArry.length<1){
            swal({title:'', text:'数据未修改!',icon: 'warning'});
            return false;
        }
        $.ajax({
            url: "updateSort",
            type: "POST",
            data: {sortArray: JSON.stringify($sortArry)},
            dataType: "json",
            cache: false,
            success: function(response) {
                if (response.type == "success") {
                    location.reload();
                }else{
                    swal({title:'', text:response.msg,icon: 'error'});
                }
            }
        });
        return false;
    });

});
</script>
<script type="text/javascript">
function typeSearch(type){
	$("#type").val(type);
	$("#listForm").submit();
}


$("#upMarket").click( function() {
	var $checkedIds = $("#listTable input[name='ids']:enabled:checked");
	if($checkedIds.length<1){
		alert("请选择商品");
		return;
	}
	if(confirm("您确定要上架吗？")){
		$.ajax({
			url: "upMarket",
			type: "POST",
			data: $checkedIds.serialize(),
			dataType: "json",
			cache: false,
			success: function(response) {
				if (response.type == "success") {
					location.reload();
				}
			}
		});
	}
	return false;
});

$("#downMarket").click( function() {
	var $checkedIds = $("#listTable input[name='ids']:enabled:checked");
	if($checkedIds.length<1){
		alert("请选择商品");
		return;
	}
	if(confirm("您确定要下架吗？")){
		$.ajax({
			url: "downMarket",
			type: "POST",
			data: $checkedIds.serialize(),
			dataType: "json",
			cache: false,
			success: function(response) {
				if (response.type == "success") {
					location.reload();
				}
			}
		});
	}
	return false;
});
</script>